(function(){
	/*局部数据*/
	var w = window,d = document,n = navigator;
	/*全局数据*/
	Co = {};

	/*浏览器检验*/
	//Co.isIE = n.userAgent.indexOf("MSIE") > -1 && !w.opera;
	Co.isIE = w.ActiveXObject ? true : false;
	//Co..isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);

	/****************************节点操作****************************/
	/*根据ID获取节点*/
	$ = function(id)
	{
		return document.getElementById(id);
	}
	/*根据tag获取节点*/
	$t = function(tag)
	{
		return document.getElementsByTagName(tag);
	}
    //取得HTML控件绝对位置
    Co.getAbsPoint = function(e){
        var x = e.offsetLeft;
        var y = e.offsetTop;
        while (e = e.offsetParent) {
            x += e.offsetLeft;
            y += e.offsetTop;
        }
        return {
            "x": x,
            "y": y
        };
    }
	/*节点池*/
	Co._ret = {};
	Co.get = function(str){
		if (Co._ret[str])
		{
			return Co._ret[str];
		}else{
			return Co._ret[str] = $(str);
		}
	}
	Co.del = function(str){
		if(Co._ret[str]){
			Co._ret[str] = null;
		}
	}
	/****************************事件机制****************************/
	Co.addEvent = function(oTarget, sType, fFuc, bUseCapture)
	{
		if(Co.isIE){
			oTarget.attachEvent("on"+sType, function(){
				fFuc.call(oTarget);
			});
		}
		else
		{
			oTarget.addEventListener(sType, fFuc, false);
		}
	}

	Co.removeEvent = function (oTarget, sType, fFuc, bUseCapture)
	{
		if(Co.isIE){
			oTarget.detachEvent("on"+sType, fFuc);
		}
		else
		{
			oTarget.removeEventListener(sType, fFuc, false);
		}
	}

	Co.getEvent = function(e)
	{
		var e = e || w.event;
		e.target || (e.target = e.srcElement);
		return e;
	}

	Co.stopEvent = function (e)
	{
		if (Co.isIE)
		{
			e.returnValue = false;
		}
		else
		{
			e.preventDefault();
		}
	}

	/****************************异步交互****************************/
	Co.ajax = {
		createXhr : function()
		{
			var factories = [
				function(){return new XMLHttpRequest();},
				function(){return new ActiveXObject("Msxml2.XMLHTTP")},
				function(){return new ActiveXObject("Microsoft.XMLHTTP")}
				]

			for(var i=0,il=factories.length;i<il;i++)
			{
				try
				{
					if(factories[i]())
					{
						return factories[i]
					}
				}
				catch (e)
				{
					continue;
				}
			}

			return function(){
				throw new Error("XMLHttpRequest not supported");
			}
			
		}(),
		get : function(sUrl,fCallback,fErrorHandler)
		{
			var oXmlhttp = this.createXhr();
			oXmlhttp.onreadystatechange = function()
			{
				if(4 == oXmlhttp.readyState)
				{
					if(200 == oXmlhttp.status)
					{
						fCallback.call(oXmlhttp);
					}
					else
					{
						if(fErrorHandler)
						{
							fErrorHandler.call(oXmlhttp);
						}
					}
				}
				
			}
			oXmlhttp.open("GET",sUrl,true);
			oXmlhttp.send(null);
		},
		post : function(sUrl,sValue,fCallback,fErrorHandler)
		{
			var oXmlhttp = this.createXhr();
			oXmlhttp.onreadystatechange = function()
			{
				if(4 == oXmlhttp.readyState)
				{
					if(200 == oXmlhttp.status)
					{
						fCallback.call(oXmlhttp);
					}
					else
					{
						if(fErrorHandler)
						{
							fErrorHandler.call(oXmlhttp);
						}
					}
				}
				
			}
			oXmlhttp.open("POST",sUrl,true);
			oXmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			oXmlhttp.send(sValue);
		}
	}

	/****************************page*******************************/
	Co.page = {
		getScrollTop : function () {
			var d = document;
			return d.documentElement.scrollTop || d.body.scrollTop;
		},
		getScrollLeft : function () {
			var d = document;
			return d.documentElement.scrollLeft || d.body.scrollLeft;
		},
		getWidth : function () {
			var doc = document, body = doc.body, html = doc.documentElement, client = doc.compatMode == 'BackCompat' ? body : doc.documentElement;
			return Math.max(html.scrollWidth, body.scrollWidth, client.clientWidth);
		},
		getHeight : function () {
			var doc = document, body = doc.body, html = doc.documentElement, client = doc.compatMode == 'BackCompat' ? body : doc.documentElement;
			return Math.max(html.scrollHeight, body.scrollHeight, client.clientHeight);
		}
	}
	/****************************sys********************************/
	Co.sys = {
		initPop : function()
		{
			var popObj = document.createElement("div");
			popObj.id = "popup";
			var tplStr = ["<div id='popheader'></div><div id='popwrap'></div>",
			"<div id='popfooter'><span id='popupBtn' style='display:block'><input type='button' id='popdownBtn' value='确 定' /></span>",
			"<span id='popconfirmBtnWrap' style='display:none'><input id='popConfirmBtn' type='button' value='确 定'/>",
			"<input id='popCancleBtn' type='button' value='取 消'/></span>",
			"</div>"];
			popObj.innerHTML = tplStr.join("");
			document.body.appendChild(popObj);
			Co.addEvent($("popdownBtn"),"click",function(){
				Co.sys.popdown();
			});
			
			var masker = document.createElement("div");
			masker.id = "masker";
			document.body.appendChild(masker);

			return {"popObj" : popObj, "masker" : masker, "popWrap" : $("popwrap")};
		},
		popup : function(str,isConfirm)
		{
			var popObj = $("popup"),popWrap,masker;
			if(!popObj)
			{
				var popEl = this.initPop();
				popObj = popEl.popObj;
				popWrap = popEl.popWrap;
				masker = popEl.masker;
			}else{
				popWrap = $("popwrap");
				masker = $("masker");
			}
			masker.style.display = "block";
			popObj.style.display = "block";
			popWrap.innerHTML = str;
			if(Co.isIE)
			{
				//网页卷去部分的长度？？？？Co.page.getScrollTop()
				masker.style.position = "absolute"
				masker.style.width = Co.page.getWidth();
				masker.style.height = Co.page.getHeight();
				masker.style.filter = "alpha(opacity=40)";
			}
			popObj.style.marginTop = Co.page.getScrollTop() - parseInt(popObj.offsetHeight/2) + "px";
			popObj.style.marginLeft = Co.page.getScrollLeft() - parseInt(popObj.offsetWidth/2)  + "px";

			var popupBtn = $("popupBtn"); 
			if(!isConfirm && popupBtn.style.display == "none"){
				popupBtn.style.display = "block";
				$("popconfirmBtnWrap").style.display = "none";
			}
		},
		popdown : function()
		{
			var popObj = $("popup"), masker = $("masker"); 
			masker.style.display = "none";
			popObj.style.display = "none";
		},
		confirm : function(str,okHanlder,cancleHanlder)
		{
			this.popup(str,true);
			var popconfirmBtnWrap = $("popconfirmBtnWrap");
			if(popconfirmBtnWrap.style.display != "block")
			{
				popconfirmBtnWrap.style.display = "block";
				$("popupBtn").style.display = "none";
			}
			var popConfirmBtn = $("popConfirmBtn");
			popConfirmBtn.onclick = function()
			{
				if(okHanlder)
				{
					okHanlder();
				}
				//Co.sys.popdown();
			}
			var popCancleBtn = $("popCancleBtn");
			popCancleBtn.onclick = function()
			{
				if(okHanlder)
				{
					Co.removeEvent(popConfirmBtn,"click",okHanlder);
				}
				if(cancleHanlder)
				{
					cancleHanlder();
				}
				Co.sys.popdown();
			};
		}
	}
})();